package cn.edu.sdyu.ie.crs.service.impl;

import cn.edu.sdyu.ie.crs.common.response.LabelValue;
import cn.edu.sdyu.ie.crs.common.response.NamedValue;
import cn.edu.sdyu.ie.crs.domain.Classroom;
import cn.edu.sdyu.ie.crs.repository.ClassroomRepository;
import cn.edu.sdyu.ie.crs.service.ClassroomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Service
public class ClassroomServiceImpl implements ClassroomService {

    @Autowired
    private ClassroomRepository classroomRepository;

    @Override
    public List<Classroom> findAll() {
        return classroomRepository.findAll();
    }

    @Override
    public Page<Classroom> findByPage(Integer page, Integer pageSize) {
        Pageable pageable = PageRequest.of(page, pageSize, Sort.by(Sort.Direction.DESC,"id"));
        return classroomRepository.findAll(pageable);
    }

    @Override
    public Classroom findClassroomById(Integer id) {
        Optional<Classroom> optional = classroomRepository.findById(id);
        if(optional.isPresent()) {
            return optional.get();
        } else {
            log.warn("指定教室信息[ID:{}]不存在", id);
            return null;
        }

        // return classroomRepository.findById(id).orElse(null);
    }

    @Override
    public void saveClassroom(Classroom classroom) {
        if(classroom.getId() == null) {
            classroom.setCreateTime(new Date());
        }
        classroomRepository.save(classroom);
    }

    @Override
    public void deleteClassroomById(Integer id) {
        classroomRepository.deleteById(id);
    }

    @Override
    public Long countClassroom() { return classroomRepository.count(); }

    @Override
    public Long countClassroomByUsageStatus(Integer usageStatus) {
        Classroom classroom = new Classroom();
        classroom.setUsageStatus(usageStatus);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("usageStatus", ExampleMatcher.GenericPropertyMatchers.exact());
        Example<Classroom> example = Example.of(classroom, matcher);
        return classroomRepository.count(example);
    }

    @Override
    public List<NamedValue> countGroupByUsageMode() {
        List<LabelValue> count = classroomRepository.countGroupByUsageMode();
        List<NamedValue> list = new ArrayList<>();
        Map<String, String> label = new HashMap<>();
        label.put("1", "教室模式");
        label.put("2", "活动室模式");
        count.stream().forEach(item -> {
            list.add(new NamedValue(label.get(item.getLabel()), item.getValue()));
        });

        return list;
    }

    @Override
    public List<Classroom> findClassroomByClassroomBuildingId(Integer classroomBuildingId) {
        return classroomRepository.findByLocation(classroomBuildingId);
    }

}
